package com.miracle.main.module.mvp.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.base.BaseFragment;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.common.util.ScreenUtils;
import com.miracle.main.R;
import com.miracle.main.R2;
import com.miracle.main.module.mvp.contract.FileContract;
import com.miracle.main.module.mvp.presenter.FilePresenter;
import com.miracle.main.module.mvp.ui.activity.UploadActivity;
import com.miracle.main.util.BuilderManager;
import com.nightonke.boommenu.Animation.BoomEnum;
import com.nightonke.boommenu.BoomButtons.BoomButton;
import com.nightonke.boommenu.BoomButtons.ButtonPlaceEnum;
import com.nightonke.boommenu.BoomMenuButton;
import com.nightonke.boommenu.ButtonEnum;
import com.nightonke.boommenu.OnBoomListener;
import com.nightonke.boommenu.Piece.PiecePlaceEnum;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * com.miracle.main.module.mvp.ui.fragment
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/2 18:14
 * @see com.miracle.main.module.mvp.ui.fragment
 */
public class FileFragment extends BaseFragment<FilePresenter> implements FileContract.View,
        TabLayout.OnTabSelectedListener, View.OnClickListener {
    @BindView(R2.id.iv_back)
    ImageView mIvBack;
    @BindView(R2.id.tv_title)
    TextView mTvTitle;
    @BindView(R2.id.tab)
    TabLayout mTabLayout;

    LifecycleProvider<FragmentEvent> lifecycleProvider;

    int mContainerId = R.id.fl_file;

    FileListFragment mAllListFragment;
    FileListFragment mPicListFragment;
    FileListFragment mDocListFragment;
    FileListFragment mNoteListFragment;

    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.cloud_fragment_file;
    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {
//        DaggerFileComponent
//                .builder()
//                .appComponent(appComponent)
//                .fileModule(new FileModule(this))
//                .build()
//                .inject(this);
    }

    @Override
    public void init(@Nullable Bundle bundle) {
        mIvBack.setVisibility(View.GONE);
        mTvTitle.setText("文件中心");
    }

    @Override
    public void initView() {
        mTabLayout.addTab(mTabLayout.newTab().setText("全部"));
        mTabLayout.addTab(mTabLayout.newTab().setText("图片"));
        mTabLayout.addTab(mTabLayout.newTab().setText("文档"));
        mTabLayout.addTab(mTabLayout.newTab().setText("笔记"));
        mTabLayout.addOnTabSelectedListener(this);
        onTabSelected(mTabLayout.getTabAt(0));
        initFragment();
    }

    private void initFragment() {

    }

    BoomMenuButton bmb;

    @Override
    public void initData() {
        bmb = mContainer.findViewById(R.id.bmb);
        assert bmb != null;
        bmb.setButtonEnum(ButtonEnum.Ham);

        bmb.setPiecePlaceEnum(PiecePlaceEnum.HAM_4);
        bmb.setButtonPlaceEnum(ButtonPlaceEnum.HAM_4);
        bmb.addBuilder(BuilderManager.getHamButtonBuilderWithDifferentPieceColor(R.drawable.icon_file_2_1,"上传文件", ""));
        bmb.addBuilder(BuilderManager.getHamButtonBuilderWithDifferentPieceColor(R.drawable.ic_icon2_ic_pic_1_1,"上传图片", ""));
        bmb.addBuilder(BuilderManager.getHamButtonBuilderWithDifferentPieceColor(R.drawable.ic_icon2_ic_pic_1_1,"上传视频", ""));
        bmb.addBuilder(BuilderManager.getHamButtonBuilderWithDifferentPieceColor(R.drawable.ic_icon2_ic_pic_1_1,"新建笔记", ""));
        bmb.setOnBoomListener(new OnBoomListener() {
            @Override
            public void onClicked(int index, BoomButton boomButton) {
                UploadActivity.start((BaseActivity) mActivity);
            }

            @Override
            public void onBackgroundClick() {

            }

            @Override
            public void onBoomWillHide() {

            }

            @Override
            public void onBoomDidHide() {

            }

            @Override
            public void onBoomWillShow() {

            }

            @Override
            public void onBoomDidShow() {

            }
        });
    }

    private List<String> getData() {
        List<String> data = new ArrayList<>();
        for (int i = 0; i < BoomEnum.values().length - 1; i++) data.add(BoomEnum.values()[i] + "");
        return data;
    }

    @Override
    public void onRequest(LifecycleProvider<FragmentEvent> lifecycleProvider) {
        this.lifecycleProvider = lifecycleProvider;
    }

    @Override
    public void showData(Object o) {

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                if (mAllListFragment == null) {
                    mAllListFragment = FileListFragment.newInstance(FileListFragment.TYPE_ALL);
                }
                mFragmentController.showOrHideFragment(mContainerId, mAllListFragment);
                break;
            case 2:
                if (mDocListFragment == null) {
                    mDocListFragment = FileListFragment.newInstance(FileListFragment.TYPE_DOC);
                }
                mFragmentController.showOrHideFragment(mContainerId, mDocListFragment);
                break;
            case 1:
                if (mPicListFragment == null) {
                    mPicListFragment = FileListFragment.newInstance(FileListFragment.TYPE_PIC);
                }
                mFragmentController.showOrHideFragment(mContainerId, mPicListFragment);
                break;
            case 3:
                if (mNoteListFragment == null) {
                    mNoteListFragment = FileListFragment.newInstance(FileListFragment.TYPE_NOTE);
                }
                mFragmentController.showOrHideFragment(mContainerId, mNoteListFragment);
                break;
            default:
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void uploadSucc() {

    }
}
